#include "loginregister.h"
#include "ui_loginregister.h"
#include<qdebug.h>
LoginRegister::LoginRegister(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LoginRegister)
{
    ui->setupUi(this); 
    setWindowFlags(Qt::FramelessWindowHint);
    ui->userNameText->setValidator(new QRegExpValidator(QRegExp("[0-9]+$")));//只能输入数字
    ui->userNameText->setPlaceholderText("请输入用户名");
    ui->lineEdit->setValidator(new QRegExpValidator(QRegExp("[0-9]+$")));//只能输入数字
    ui->lineEdit->setEchoMode(QLineEdit::Password);//以黑点显示
    ui->lineEdit->setPlaceholderText("请输入密码");
//    ui->lineEdit_2->setPlaceholderText("请输入邮箱");

}

LoginRegister::~LoginRegister()
{
    delete ui;
}

void LoginRegister::on_registerButton_clicked()
{

           reg.show();
           this->hide();
           this->setDisabled(false);
}

void LoginRegister::on_loginButton_clicked()
{
        DatabaseMysql datas;
        this->db=datas.db;
        this->query=datas.query;
        string account=this->ui->userNameText->text().toStdString();
        string password = this->ui->lineEdit->text().toStdString();
        bool isHave = false;
        //ODBC查询数据
        QSqlQuery result = db.exec(" select * from users");
        while(result.next())
        {
            string Account = result.value("username").toString().toStdString();
            string Password = result.value("userpassword").toString().toStdString();
            QString s;
            if(account.compare(Account) == 0)
            {
                isHave = true;
                if(password.compare(Password) == 0)
                {
                    QMessageBox::about(this," ",QString::fromUtf8("登录成功"));
                    this->close();
                    game *g = new game;                    
                    g->db = this->db;
                    g->query = this->query;
                    g->show();
                    g->uID=Account;

                }else
                {
                    QMessageBox::warning(NULL, "提示", "账户与密码不匹配");
                }
            }
        }
        if(!isHave)
        {
             QMessageBox::warning(NULL, "提示", "不存在该账户");
//             this->close();
//             game *g = new game;
//             g->show();

        }

        db.close();

}


void LoginRegister::on_btn_exit_clicked()
{
    exit(0);
}

void LoginRegister::on_btn_Visitor_clicked()
{
    this->close();
    game *g = new game;
    g->show();

}
